//
//  Team9Tool.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/16.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9Tool.h"
#import "Team9Header.h"
#import <UIImageView+WebCache.h>
#import <objc/runtime.h>

#import "Team9ExpertUsersVc.h"
#import "Team9workVc.h"
#import "Team9DesignerVc.h"
#import "JQAFNetworkTool.h"
#import "ConvertToCommonEmoticonsHelper.h"
#import <MBProgressHUD.h>
#import <Masonry.h>

@implementation Team9Tool
#pragma height for text
+ (CGFloat)heightByFloatSize:(CGFloat)fontSize
                        With:(NSString *)text
{
    return [self heightByFloatSize:fontSize width:WIDTH - 20 With:text];;
}
+ (CGFloat)heightByFloatSize:(CGFloat)fontSize
                       width:(CGFloat)width
                        With:(NSString *)text
{
    NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSize], NSFontAttributeName, nil];
    // 去掉表情的三方
    NSString *string = [ConvertToCommonEmoticonsHelper convertToCommonEmoticons:text];
    CGRect rect = [string boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    
    return rect.size.height;
}
// 没有占位图 直接给333 有不变 调用自己的详细方法
+ (void)sd_imageView:(UIImageView *)imageView
               image:(NSString *)str
    placeholderImage:(NSString *)placeholder
{
    UIImage *placeholderImage = nil;
    if (placeholder) {
        placeholderImage = [UIImage imageNamed:placeholder];
    } else placeholderImage = [UIImage imageNamed:@"333"];
    [self sd_imageView:imageView image:str placeholder:placeholderImage];
}
// 详细的sd完成之后 出现动画
+ (void)sd_imageView:(UIImageView *)imageView
               image:(NSString *)str
         placeholder:(UIImage *)placeholder{
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (cacheType == SDImageCacheTypeDisk) {
            [UIView animateWithDuration:0.3 animations:^{
                CATransition *transition = [CATransition animation];
                transition.type = @"fade";
                [transition setDuration:0.5];
                [transition setRepeatCount:1];
                // 向imageView上添加动画效果
                [imageView.layer addAnimation:transition forKey:@"transition"];
            }];
        }
    }];
}
/* 关联对象 runtime 把一个progressView 达到一个imageView的效果
    然后把他添加到imageView上
    调用方法的时候先移除之前的 进度条 创建个新的
    进度条的方法要gcd返回主线程
    注:sd imageView 的方法看不懂请先上github下载demo
 */
static void *progressImageView = "progressImageViewKey";
+ (UIProgressView *)progressImageView:(UIImageView *)imageView{
    UIProgressView *progress = [[UIProgressView alloc] init];
    [imageView addSubview:progress];
    objc_setAssociatedObject(imageView, progressImageView, progress, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [progress setTintColor:[UIColor lightGrayColor]];
    [progress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(imageView);
        make.left.mas_equalTo(imageView).offset(20);
        make.height.equalTo(@20);
    }];
    return progress;
}
+ (void)sd_progress_imageView:(UIImageView *)imageView
                        image:(NSString *)str
             placeholderImage:(NSString *)placeholder{
    UIProgressView *progress = objc_getAssociatedObject(imageView, progressImageView);
    if (progress) {
        [progress removeFromSuperview];
        objc_setAssociatedObject(imageView, progressImageView, nil, OBJC_ASSOCIATION_RETAIN);
    }
    progress = [Team9Tool progressImageView:imageView];
    [imageView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:placeholder] options: SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        dispatch_async(dispatch_get_main_queue(), ^{
           [progress setProgress:(double)receivedSize / expectedSize animated:YES];
        });
        
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [progress removeFromSuperview];
    }];
}
// str 属性 没用写错了,调用了一个sdimageView的方法,看不懂去看demo
+ (void)sd_imageView:(UIImageView *)imageView
               image:(NSString *)str
    placeholderImage:(NSString *)placeholder
         detailImage:(NSString *)detailStr
{
        [imageView sd_setImageWithURL:[NSURL URLWithString:detailStr] placeholderImage:[UIImage imageNamed:placeholder] options:SDWebImageLowPriority | SDWebImageRetryFailed | SDWebImageProgressiveDownload];
}
// 关联对象 添加手势,实现跳转
/*
 参数多的方法 调用参数少的方法
 */
#pragma mark methods
static void *imageViewKey = "imageViewTapKey";

+ (void)imageView:(UIImageView *)imageView
             type:(NSString *)type
               Id:(NSString *)Id
   viewController:(UIViewController *)VC
         baiHiden:(BOOL)hiden
{
    imageView.userInteractionEnabled = YES;
    __weak __typeof(VC)weakVC = VC;
    void(^block)() = ^{
        [self viewContrller:weakVC pushBytype:type andId:Id barHiden:hiden];
    };
    objc_setAssociatedObject(imageView, imageViewKey, block, OBJC_ASSOCIATION_COPY);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [imageView addGestureRecognizer:tap];
}
+ (void)tapAction:(UIGestureRecognizer *)tap{
   void(^block)() = objc_getAssociatedObject(tap.view, imageViewKey);
    block();
    NSLog(@"test by me");
}
+ (void)imageView:(UIImageView *)imageView
             type:(NSString *)type
               Id:(NSString *)Id
   viewController:(UIViewController *)VC {
  [self imageView:imageView type:type Id:Id viewController:VC baiHiden:NO];
}
+ (void)viewContrller:(UIViewController *)VC
           pushBytype:(NSString *)type
                andId:(NSString *)Id
            barHiden:(BOOL)hiden{
    NSString *url = [NSString stringWithFormat:@"http://api.meilijia.com/app/user_home_page?user_id=%@", Id];
    Team9ExpertUsersVc *vc = [Team9ExpertUsersVc new];
    vc.url = url;
    vc.Id = Id;
    Team9workVc *workVc = [Team9workVc new];
    workVc.url = url;
    workVc.Id =Id;
    Team9DesignerVc *designerVc = [Team9DesignerVc new];
    designerVc.url = url;
    designerVc.Id = Id;
    switch (type.intValue) {
        case 0:
            VC.hidesBottomBarWhenPushed = YES;
            [VC.navigationController pushViewController:vc animated:YES];
            if (hiden) {
                VC.hidesBottomBarWhenPushed = NO;
            }
            break;
        case 1:
            VC.hidesBottomBarWhenPushed = YES;
            [VC.navigationController pushViewController:designerVc animated:YES];
            if (hiden) {
                VC.hidesBottomBarWhenPushed = NO;
            }
            break;
        case 2:
            VC.hidesBottomBarWhenPushed = YES;
            [VC.navigationController pushViewController:workVc animated:YES];
            if (hiden) {
                VC.hidesBottomBarWhenPushed = NO;
            }
            break;
        default:
            break;
    }
}

+ (void)viewContrller:(UIViewController *)VC
           pushBytype:(NSString *)type
                andId:(NSString *)Id{
    [self viewContrller:VC pushBytype:type andId:Id barHiden:NO];
}
static void *imageViewKeyNet = "imageViewTapKeyNet";
+ (void)imageView:(UIImageView *)imageView
               Id:(NSString *)Id
   viewController:(UIViewController *)VC
         barHiden:(BOOL)hiden{
    imageView.userInteractionEnabled = YES;
    __weak __typeof(VC)weakVC = VC;
    void(^block)() = ^{
        NSString *url = [NSString stringWithFormat:@"http://api.meilijia.com/app/user_home_page?user_id=%@", Id];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hud.labelText = @"正在查询类型,请等待";
        [hud show:YES];
        [JQAFNetworkTool getUrl:url success:^(id result) {
            NSString *type = result[@"data"][@"user_home_info"][@"user_info"][@"user_type"];
            NSLog(@"%@", type);
            [self viewContrller:weakVC pushBytype:type andId:Id barHiden:hiden];
            [hud hide:YES];
        }];
    };
    objc_setAssociatedObject(imageView, imageViewKeyNet, block, OBJC_ASSOCIATION_COPY);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapNetAction:)];
    [imageView addGestureRecognizer:tap];
}
+ (void)tapNetAction:(UIGestureRecognizer *)tap{
    void(^block)() = objc_getAssociatedObject(tap.view, imageViewKeyNet);
    block();
    NSLog(@"test by you");
}




@end
